package org.crumbleworks.crumbchat.client;

import org.crumbleworks.crumbchat.client.controller.ClientControllerInterface;

public class InputParser {
    private final String CONNECT_COMMAND = "connect";
    private final String DISCONNECT_COMMAND = "disconnect";
    private final String SHUTDOWN_COMMAND = "quit";

    private ClientControllerInterface controller;

    public InputParser(ClientControllerInterface controller) {
        this.controller = controller;
    }

    public void parseInput(String input) {
        if (input.startsWith("/")) {
            input = input.substring(1);
            String[] splittedInput = input.split(" ");
            String command = splittedInput[0];

            if (command.equals(CONNECT_COMMAND)) {
                controller.connectToServer(splittedInput[1], splittedInput[2]);
            }
            else if (command.equals(DISCONNECT_COMMAND)) {
                controller.disconnectFromServer(false);
            }
            else if (command.equals(SHUTDOWN_COMMAND)) {
                controller.disconnectFromServer(true);
            }
        }
        else {
            controller.sendMessageToServer(input);
        }
    }
}
